***This do-file runs the regressions found in table 4 of the paper
**Curtis RESTAT
**Change to directory where data and code were downloaded
**cd 


clear all
set matsize 9000
set maxvar 9000


use cbp_cty_table4,clear

do datacleancty_table4.do

	rename state stateabb
	rename fipstate state
	so state year

drop if year==2009
replace trend=year-1997

qui tab naics,gen(ind)

forvalues i = 1(1)21 {
   qui replace ind`i'= ind`i'*trend
}

forvalues i = 1(1)21 {
   qui rename ind`i' indtrend`i'
 }

drop if inlist(ctyid,"1481033","1481269","1481301","1481261")

qui tab state,gen(STdum)

foreach x of varlist STdum1-STdum49 {
  qui gen trend`x'=`x'*trend
  }


so ctyind year
gen empweight2=emp_imp if year==1998
bysort ctyind:carryforward empweight2,replace
qui tab year,gen(y)
drop if emp==.
gen NBPstr="NBP" if NBP==1
replace NBPstr="west" if NBPstr==""
gen NBPind=NBPstr+naics

***Merging in directly regulated cnty-industries
	drop naics3
	gen naics3=substr(naics,1,3)
	gen cntyind=fips+naics3
	so cntyind
	merge m:1 cntyind using directreg_cntyind_table4
	replace nbp_reg=0 if nbp_reg==.
		
	gen postxnbp_reg=post*nbp_reg
	egen cntydir=max(nbp_reg),by(fips)
	egen cntyindreg=max(nbp_reg),by(cntyind)
	gen postxcntydir=post*cntydir
	gen industryregind=1 if inlist(naics3,"322","324","325","331","327")
	replace industryregind=0 if industryregind==.
	gen postxNBPxindreg=post*industryregind*NBP
	gen postxindreg=post*industryregind
	gen indregxNBP=NBP*industryregind
	gen indregxcntydir=industryregind*cntydir

	gen temp_dir=cntyind
	replace temp_dir="" if nbp_reg==0 |nbp_reg==.
	qui tab temp_dir,gen(cnty_spec)
	forvalues i = 1(1)206 {
		qui gen spec`i'=cnty_spec`i'*post
		cap qui replace spec`i'=0 if spec`i'==.
		cap qui replace cnty_spec`i'=0 if cnty_spec`i'==.
		}

qui tab yearind,gen(yi)
forvalues i = 1(1)21 {
   qui gen eindtrend`i' = indtrend`i'*NBP
  }

forvalues i = 1(1)21 {
   qui gen windtrend`i' = indtrend`i'*((NBP-1)^2)
  }



**** Controls 1: Regs with Direct Reg Mechanism 
	qui eststo: qui areg  lemp_imp postxNBPxint postxnbp_reg postxcntydir postxNBP postxint intxNBP  y1-y11[aweight=empweight2], abs(ctyind) rob cluster(NBPind)

	qui eststo: qui areg  lemp_imp postxNBPxint postxnbp_reg postxcntydir postxNBP postxint intxNBP  y1-y11 indtrend* trendSTdum* [aweight=empweight2], abs(ctyind) rob cluster(NBPind)

	qui eststo: qui areg  lemp_imp postxNBPxint postxnbp_reg postxcntydir postxNBP postxint intxNBP yi* trendSTdum* [aweight=empweight2], abs(ctyind) rob cluster(NBPind)

	qui eststo: qui areg  lemp_imp postxNBPxint postxnbp_reg postxcntydir postxNBP postxint intxNBP windtrend* eindtrend* y1-y11 [aweight=empweight2], abs(ctyind) rob cluster(NBPind)

	qui eststo: qui areg  lemp_imp postxNBPxint postxnbp_reg postxcntydir postxNBP postxint intxNBP windtrend* eindtrend* yi* trendSTdum* [aweight=empweight2], abs(ctyind) rob cluster(NBPind)
	
	qui eststo: qui areg  lemp_imp postxNBPxint postxnbp_reg postxcntydir postxNBP postxint intxNBP windtrend* eindtrend* y1-y11 indtrend* trendSTdum* [aweight=empweight2], abs(ctyind) rob cluster(NBPind)

outreg2 [est1 est2 est3 est4 est5 est6] using "Table4a.rtf",keep(postxNBPxint postxNBP postxnbp_reg postxcntydir) nocons replace

eststo clear

***setting interaction var = 0 assumes directly regulated cnty-inds were only impacted through direct regulation
    replace postxNBPxint=0 if nbp_reg==1
**** Controls 1: Regs with Direct Reg Mechanism 
	qui eststo: qui areg  lemp_imp postxNBPxint postxnbp_reg postxcntydir postxNBP postxint intxNBP  y1-y11[aweight=empweight2], abs(ctyind) rob cluster(NBPind)

	qui eststo: qui areg  lemp_imp postxNBPxint postxnbp_reg postxcntydir postxNBP postxint intxNBP  y1-y11 indtrend* trendSTdum* [aweight=empweight2], abs(ctyind) rob cluster(NBPind)

	qui eststo: qui areg  lemp_imp postxNBPxint postxnbp_reg postxcntydir postxNBP postxint intxNBP yi* trendSTdum* [aweight=empweight2], abs(ctyind) rob cluster(NBPind)

	qui eststo: qui areg  lemp_imp postxNBPxint postxnbp_reg postxcntydir postxNBP postxint intxNBP windtrend* eindtrend* y1-y11 [aweight=empweight2], abs(ctyind) rob cluster(NBPind)
	
	qui eststo: qui areg  lemp_imp postxNBPxint postxnbp_reg postxcntydir postxNBP postxint intxNBP windtrend* eindtrend* yi* trendSTdum* [aweight=empweight2], abs(ctyind) rob cluster(NBPind)

	qui eststo: qui areg  lemp_imp postxNBPxint postxnbp_reg postxcntydir postxNBP postxint intxNBP windtrend* eindtrend* y1-y11 indtrend* trendSTdum* [aweight=empweight2], abs(ctyind) rob cluster(NBPind)


outreg2 [est1 est2 est3 est4 est5 est6] using "Table4b.rtf",keep(postxNBPxint postxNBP postxnbp_reg postxcntydir) nocons replace

eststo clear

